package com.it.nongchanping.controller;

import com.it.nongchanping.entity.Company;
import com.it.nongchanping.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/admin/company")
public class AdminCompanyController {

    @Autowired
    private CompanyService companyService;

    @GetMapping("")
    public String showCompanyForm(Model model) {
        Company companyInfo = companyService.getCompanyInfo();
        if (companyInfo == null) {
            companyInfo = new Company();
        }
        model.addAttribute("company", companyInfo);
        model.addAttribute("title", "企业信息管理");
        model.addAttribute("activePage", "company");
        return "admin/company/form";
    }

    @PostMapping("/save")
    public String saveCompanyInfo(@ModelAttribute Company company, RedirectAttributes redirectAttributes) {
        companyService.saveCompanyInfo(company);
        redirectAttributes.addFlashAttribute("message", "企业信息保存成功！");
        return "redirect:/admin/company";
    }
} 